package com.bingxue.edu.framework.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import io.micrometer.common.util.StringUtils;

public class ExcelBooleanConverter implements Converter<Boolean> {
    public ExcelBooleanConverter() {
    }

    public Class<?> supportJavaTypeKey() {
        return Boolean.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public WriteCellData<?> convertToExcelData(Boolean value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(value ? "是" : "否");
    }

    @Override
    public Boolean convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                        GlobalConfiguration globalConfiguration) {
        String value = cellData.getStringValue();
        if (StringUtils.isBlank(value)){
            // 未知
            return null;
        }
        if (value.contains("是")) {
            return Boolean.TRUE;
        }
        if (value.contains("否")) {
            return Boolean.FALSE;
        }
        return null;
    }

}
